package com.sqfw.project.learnRecord.service.impl;

import com.sqfw.common.utils.DateUtils;
import com.sqfw.common.utils.StringUtils;
import com.sqfw.project.learnRecord.domain.LearnRecordInfo;
import com.sqfw.project.learnRecord.mapper.LearnRecordInfoMapper;
import com.sqfw.project.learnRecord.service.ILearnRecordInfoService;
import com.sqfw.project.wp.utils.FtpUtil;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 录音学习Service业务层处理
 *
 * @author zsj
 * @date 2024-09-23
 */
@Service
public class LearnRecordInfoServiceImpl implements ILearnRecordInfoService
{
    @Autowired
    private LearnRecordInfoMapper learnRecordInfoMapper;

    @Value("${ftp.host}")
    private String FTP_HOST;
    @Value("${ftp.port}")
    private Integer FTP_PORT;
    @Value("${ftp.username}")
    private String FTP_USERNAME;
    @Value("${ftp.password}")
    private String FTP_PASSWORD;

    /**
     * 查询录音学习
     *
     * @param id 录音学习主键
     * @return 录音学习
     */
    @Override
    public LearnRecordInfo selectLearnRecordInfoById(Long id)
    {
        return learnRecordInfoMapper.selectLearnRecordInfoById(id);
    }

    /**
     * 查询录音学习列表
     *
     * @param learnRecordInfo 录音学习
     * @return 录音学习
     */
    @Override
    public List<LearnRecordInfo> selectLearnRecordInfoList(LearnRecordInfo learnRecordInfo)
    {
        //内容分类查询条件
        List<List<String>> classAllFind = learnRecordInfo.getClassAllFind();
        //内容分类查询
        if (StringUtils.isEmpty(classAllFind)) {
            learnRecordInfo.setClassAll(null);
        } else {
            List<String> classAllCon = new ArrayList<>();
            for (List<String> strings : classAllFind) {
                classAllCon.add(strings.get(strings.size() - 1));
            }
            learnRecordInfo.setClassAll(classAllCon);
        }
        return learnRecordInfoMapper.selectLearnRecordInfoList(learnRecordInfo);
    }

    /**
     * 新增录音学习
     *
     * @param learnRecordInfo 录音学习
     * @return 结果
     */
    @Override
    public int insertLearnRecordInfo(LearnRecordInfo learnRecordInfo)
    {
        learnRecordInfo.setCreateTime(DateUtils.getNowDate());
        return learnRecordInfoMapper.insertLearnRecordInfo(learnRecordInfo);
    }

    /**
     * 修改录音学习
     *
     * @param learnRecordInfo 录音学习
     * @return 结果
     */
    @Override
    public int updateLearnRecordInfo(LearnRecordInfo learnRecordInfo)
    {
        return learnRecordInfoMapper.updateLearnRecordInfo(learnRecordInfo);
    }

    /**
     * 批量删除录音学习
     *
     * @param ids 需要删除的录音学习主键
     * @return 结果
     */
    @Override
    public int deleteLearnRecordInfoByIds(Long[] ids)
    {
        return learnRecordInfoMapper.deleteLearnRecordInfoByIds(ids);
    }

    /**
     * 删除录音学习信息
     *
     * @param id 录音学习主键
     * @return 结果
     */
    @Override
    public int deleteLearnRecordInfoById(Long id)
    {
        return learnRecordInfoMapper.deleteLearnRecordInfoById(id);
    }

    public byte[] playRecord(String fileName) throws Exception {
        FTPClient ftpClient = FtpUtil.getFtpClient(FTP_HOST, FTP_PORT, FTP_USERNAME, FTP_PASSWORD);
        String remoteFile = new String(fileName.getBytes(StandardCharsets.UTF_8), FTP.DEFAULT_CONTROL_ENCODING);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        boolean success = ftpClient.retrieveFile(remoteFile, outputStream);
        ftpClient.logout();
        ftpClient.disconnect();

        if (success) {
            return outputStream.toByteArray();
        } else {
            throw new IOException("Failed to download file.");
        }
    }
}
